package com.perfma.xtest.tool;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: junhui.si
 * @date: 2020-11-10 17:41
 **/
public class TimeDemo {

    public static void main(String[] args) throws InterruptedException, ParseException {
//        while (true) {
//            long nano = LocalDateTime.now().getLong(ChronoField.NANO_OF_SECOND);
//            System.out.println(nano);

//            LocalDateTime localDateTime1 = LocalDateTime.now();
//            Thread.sleep(1);
//            LocalDateTime localDateTime2 = LocalDateTime.now();
//            Duration d = Duration.between(localDateTime1, localDateTime2);
//            if (d.toNanos() % 1000000 != 0) {
//                System.out.println(d.toNanos());
//            }
//            long t1 = localDateTime1.getLong(ChronoField.NANO_OF_SECOND);
//            long t2 = localDateTime2.getLong(ChronoField.NANO_OF_SECOND);
//            long dd = t2 - t1;
//            if (dd < 0) {
//                System.out.println("*******");
//                System.out.println(t1);
//                System.out.println(t2);
//                System.out.println(dd);
//            }
//        }
//        x();
//        System.out.println();

//        System.out.println((double) 1605084052000123L / 1000);
//        BigDecimal bigDecimal = new BigDecimal(1605084052000123L);
//        System.out.println(bigDecimal.divide(new BigDecimal(1000)).doubleValue());
//        Double d = 0.3d;
//        BigDecimal bigDecimal = new BigDecimal(d);
//        System.out.println(bigDecimal+"");
//        System.out.println(bigDecimal.divide(new BigDecimal(10000), 6, BigDecimal.ROUND_HALF_UP).doubleValue());
//        BigDecimal bd = new BigDecimal("123.000000");
//        String resultStr = bd.setScale(3, BigDecimal.ROUND_HALF_UP).toString();
//        System.out.println(resultStr);
        Double d = null;
        String s = String.valueOf(d);
        System.out.println(s);
    }

    private static void x() throws ParseException {
        {
            String valueIn = "2019-02-19T23:28:04.434410+0800";
            String formatIn = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";
            String formatOut = "yyyy-MM-dd HH:mm:ss";
            String formatOut2 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";

            //使用SimpleDateFormat解析
            Date date = new SimpleDateFormat(formatIn).parse(valueIn);
            String out1 = new SimpleDateFormat(formatOut).format(date);
            System.out.println("SimpleDateFormat out:" + out1 + ",会多几分钟");

            //使用LocalDateTime
            //先获取时间纳秒
            LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
            ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
            String out2 = DateTimeFormatter.ofPattern(formatOut).format(zdt);
            System.out.println("LocalDateTime out:" + out2);

            //double类型时间2019-02-19T23:28:04.434410+0800=1550590084.434410还原yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ
            double d_time = 1550590084.434410;
            BigDecimal bd = new BigDecimal(d_time).setScale(6, BigDecimal.ROUND_DOWN);
            long Millitime = (long) (d_time * 1000);
            int Nano = Integer.parseInt(bd.toString().substring(bd.toString().indexOf(".") + 1) + "000");

            Date date1 = new Date(Millitime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int YEAR = calendar.get(Calendar.YEAR);
            int MONTH = calendar.get(Calendar.MONTH) + 1;
            int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
            int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
            int MINUTE = calendar.get(Calendar.MINUTE);
            int SECOND = calendar.get(Calendar.SECOND);

            LocalDateTime ldt2 = LocalDateTime.of(YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, SECOND, Nano);
            //必须要指定时区，要不然会报java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
            ZonedDateTime zdt2 = ZonedDateTime.of(ldt2, ZoneId.systemDefault());
            String out3 = DateTimeFormatter.ofPattern(formatOut2).format(zdt2);
            System.out.println("LocalDateTime 1550590084.434410 out:" + out3);
        }
    }
}
